#!/bin/bash

if git diff --cached --name-only | grep -q '^src-tauri/'; then
    cargo clippy --manifest-path ./src-tauri/Cargo.toml
    if [ $? -ne 0 ]; then
        echo "Clippy found issues in src-tauri. Please fix them before pushing."
        exit 1
    fi
fi

remote_name="$1"
remote_url=$(git remote get-url "$remote_name")

if [[ "$remote_url" =~ github\.com[:/]+clash-verge-rev/clash-verge-rev(\.git)?$ ]]; then
    echo "[pre-push] Detected push to clash-verge-rev/clash-verge-rev ($remote_url)"
    echo "[pre-push] Running pnpm format:check..."

    pnpm format:check
    if [ $? -ne 0 ]; then
        echo "❌ Code format check failed. Please fix formatting before pushing."
        exit 1
    fi
else
    echo "[pre-push] Not pushing to target repo. Skipping format check."
fi

exit 0
